package com.xw.controller;

import com.xw.common.Constants;
import com.xw.common.Result;
import com.xw.entity.Message;
import com.xw.service.WebSocketServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @Description: TODO
 * @Date: 2024/12/18 16:52
 * @Version: 1.0
 * @author 86151
 */
@RestController
@RequestMapping("/websocket")
public class WebSocketController {

    @Autowired
    private WebSocketServerService webSocketServerService;

    /**
     * 发送消息
     *
     * @param message 消息对象
     * @return 发送结果
     */
    @PostMapping("/message")
    public Result<?> sendMessage(@RequestBody Message message) {
        Integer sendType = message.getSendType();
        switch (sendType) {
            case 1:
                // 给指定用户发送消息
                try {
                    webSocketServerService.sendMessage(message.getUserId(), message.getContent());
                } catch (Exception e) {
                    return new Result<>(Constants.ERROR, "发送消息失败");
                }
                break;
            case 2:
                // 群发消息，包括自己
                webSocketServerService.sendMessageAll(message.getContent());
                break;
            case 3:
                // 群发消息，不包括自己
                try {
                    webSocketServerService.sendMessageAll(message.getContent(), message.getUserId());
                } catch (Exception e) {
                    return new Result<>(Constants.ERROR, "发送消息失败");
                }
                break;
            default:
            return new Result<>(Constants.ERROR, "发送消息类型有误");
        }
        return new Result<>(Constants.SUCCESS,"发送成功");
    }
}